package com.itboye.zhujia;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.itboye.zhujia.adapter.HouseAdapter;
import com.itboye.zhujia.adapter.ListAdapter;
import com.itboye.zhujia.bean.HouseBean;
import com.itboye.zhujia.custom.CarouselView;
import com.itboye.zhujia.custom.MyListView;
import com.itboye.zhujia.custom.XGridView;
import com.itboye.zhujia.custom.XScrollView;
import com.itboye.zhujia.homefragment.HomeSearchToActivity;
import com.itboye.zhujia.util.ByAlert;
import com.itboye.zhujia.util.XImageLoader;

public class HomeFragment extends Fragment implements OnClickListener{

	CarouselView banner_top;
	private ArrayList<ImageView> bmps;
	private int position;
	XScrollView scrollView;
	HouseAdapter adapter;
	
	BaseAdapter adapter2;
	MyListView home_listview;
	EditText edit_txt;
	ArrayList<HouseBean> arrayListHouse=new ArrayList<>();
	
	
	private TextView tvTenant,tvservice,tvlandlady;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View messageLayout = inflater.inflate(R.layout.fragment_home, container,
				false);
		return messageLayout;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	
//		banner_top = (CarouselView) getView().findViewById(R.id.banner_top);
		home_listview=(MyListView) getView().findViewById(R.id.mylistview);
		
		adapter2=new ListAdapter(getActivity());
		home_listview.setAdapter(adapter2);
//		edit_txt=(EditText) getView().findViewById(R.id.edit_txt);
//		edit_txt.setOnClickListener(this);
//		getTopBanner();
//		getListViewData();
//		//这几个刷新Label的设置
//		scrollView = (XScrollView) getView().findViewById(R.id.scrollView);
//		scrollView.setPullLoadEnabled(true);
//		scrollView.setPullRefreshEnabled(true);
//		ScrollView scrollView2=scrollView.getRefreshableView();
		
//		scrollView.setLastUpdatedLabel("lastUpdateLabel"); 
		// XPicasso.loadImage(getActivity(),img,
		// "http://image.baidu.com/search/detail?ct=503316480&z=undefined&tn=baiduimagedetail&ipn=d&word=%E5%9B%BE%E7%89%87&step_word=&ie=utf-8&in=&cl=2&lm=-1&st=undefined&cs=1808529296,2001246714&os=3819464713,2631741104&simid=3480550331,229326920&pn=520&rn=1&di=38390801460&ln=1945&fr=&fmq=1472285358727_R&fm=&ic=undefined&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=&istype=0&ist=&jit=&bdtype=0&adpicid=0&pi=&gsm=1e0&objurl=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2013%2F228%2F9D3EW9221H45.jpg&rpstart=0&rpnum=0&adpicid=0");
	
		tvTenant=(TextView) getActivity().findViewById(R.id.tvTenant);
		tvservice=(TextView) getActivity().findViewById(R.id.tvservice);
		tvlandlady=(TextView) getActivity().findViewById(R.id.tvlandlady);
		
//		tvTenant.setOnClickListener(this);
//		tvservice.setOnClickListener(this);
//		tvlandlady.setOnClickListener(this);
	}

	private void getListViewData() {
		// TODO Auto-generated method stub
		adapter=new HouseAdapter(getActivity(), arrayListHouse);
		home_listview.setAdapter(adapter);
		for (int i = 0; i < 210; i++) {
			HouseBean bean=new HouseBean();
			arrayListHouse.add(bean);
		}
		adapter.notifyDataSetChanged();
	}

	private void getTopBanner() {
		// TODO Auto-generated method stub
		bmps = new ArrayList<ImageView>();
		for (int i = 0; i < 10; i++) {
			position = i;
			ImageView imageView = new ImageView(getActivity());
			imageView.setScaleType(ScaleType.FIT_XY);
			XImageLoader
					.load("http://imgsrc.baidu.com/forum/pic/item/37e8a0773912b31b59da2ce98618367adbb4e126.jpg",
							imageView);
			bmps.add(imageView);
			imageView.setTag(i);
			imageView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					// Intent intent = new Intent(getActivity(),
					// WebActivity.class);
					// intent.putExtra("title", boModels.get((int)
					// v.getTag()).getTitle());
					// intent.putExtra("url",
					// boModels.get((int) v.getTag()).getUrl());
					// startActivity(intent);
				}
			});
		}
		banner_top.setImageBitmaps(bmps);
		// //让顶部控件获取到焦点这样滚动条就自然停在顶部了
		banner_top.setFocusable(true);
		banner_top.setFocusableInTouchMode(true);
		banner_top.requestFocus();
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		Intent intent =null; 
		switch (arg0.getId()) {
		case R.id.edit_txt:
			intent=new Intent(getActivity(),HomeSearchToActivity.class);
			startActivity(intent);
			break;
		case R.id.tvTenant:
			tvTenant.setBackgroundResource(getResources().getColor(R.color.white));
			tvservice.setBackgroundResource(getResources().getColor(R.color.dray_gray));
			tvlandlady.setBackgroundResource(getResources().getColor(R.color.dray_gray));
			ByAlert.alert("");
			break;
		case R.id.tvservice:
			tvTenant.setBackgroundResource(getResources().getColor(R.color.dray_gray));
			tvservice.setBackgroundResource(getResources().getColor(R.color.white));
			tvlandlady.setBackgroundResource(getResources().getColor(R.color.dray_gray));
			ByAlert.alert("");
			break;
		case R.id.tvlandlady:
			tvTenant.setBackgroundResource(getResources().getColor(R.color.dray_gray));
			tvservice.setBackgroundResource(getResources().getColor(R.color.dray_gray));
			tvlandlady.setBackgroundResource(getResources().getColor(R.color.white));
			break;

		default:
			break;
		}
	}

}
